# study code
#
# Block
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0207
#
# start  2011/10/10 4:21
# parser 2011/10/10 4:44
# end    2011/10/10 6:14
class Block
    attr_accessor :color,:direction,:left,:top,:right,:bottom

    def initialize( c ,d, x, y )
        @color,@direction,@left,@top = c, d, x, y
        @right = x + (d == 1 ? 2 : 4)
        @bottom = y + (d == 1 ? 4 : 2)
    end

    def pos_include?(x,y)
        return x >= @left && x <= @right && y >= @top && y <= @bottom
    end

    def connect?(other)
        if( (@left - other.right).abs == 1 || (@right - other.left).abs == 1)
            return @top <= other.bottom || @bottom >=other.top
        elsif( (@top - other.bottom).abs == 1 || (@bottom - other.top).abs == 1)
            return @left <= other.right || @right >= other.top
        else
            return false
        end
    end
end


class Context
    attr_accessor :width,:height,:xs,:ys,:xg,:yg,:block_num,:blocks
    def initialize
        @blocks = []
    end
end


def parse_input( str )
    input = []
    i = 0
    context = nil
    str.split("\n").each{|line|
        if line == "0 0"
            break
        end
        case i
        when 0
            context = Context.new
            w,h = line.split(" ")
            p w,h
            context.width = w.to_i
            context.height = h.to_i
        when 1
            xs,ys = line.split(" ")
            p xs,ys
            context.xs = xs.to_i
            context.ys = ys.to_i
        when 2
            xg,yg = line.split(" ")
            p xg,yg
            context.xg = xg.to_i
            context.yg = yg.to_i
        when 3
            context.block_num = line.to_i
            p context.block_num
        else
            block = Block.new( *(line.split(" ").map{|v| v.to_i } ) )
            context.blocks.push( block )
            if( i == (3 + context.block_num) ) 
                i = -1
                input.push( context )
            end
        end
        i+=1
    }
    return input
end


def solve( ctx )
    #puts "solve" + ctx.inspect
    start = ctx.blocks.select{|b| b.pos_include?(ctx.xs,ctx.ys) }[0]
    goal  = ctx.blocks.select{|b| b.pos_include?( ctx.xg, ctx.yg )}[0]
    color = start.color
    loads = ctx.blocks.select{|b| (b.color == color) && (b != start) && (b != goal) }
    ret = search( Node.new([start], loads, goal ) )
    puts ret ? "OK" : "NG"
end



class Node
    attr_accessor :path,:childs,:goal

    def initialize( path, blocks, goal )
        @path = path
        @blocks = blocks
        @goal = goal
    end

    def next
        if(childs == nil)
            @c_index = 0
            childs = []
            (@blocks - @path).each{|v|
                if(path.last.connect?(v))
                   new_path = Array.new @path
                   new_path.push( v )
                   childs.push( Node.new( new_path, @blocks, @goal ) )
                end
            }
        end

        if(childs.length < @c_index )
            @c_index += 1
            return childs[@c_index - 1]
        else
            return nil
        end
    end

    def check_goal
        @goal.connect?(@path.last)
    end
end


def search( start )
    stack = []
    stack.push( start )
    while stack.length != 0 do
        node = stack.last
        if node.check_goal
            return stack
        else
            child = node.next
            if child == nil
                stack.pop
            else
                stack.push( child )
            end
        end
    end
    return nil

end

INPUT_DATA = <<DATA
20 20 
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
DATA
ctxs = parse_input( INPUT_DATA )
ctxs.each{|c|
solve( c )
}
